<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>编译过程 | hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="编译过程graph TB  subgraph 代码生成图         E(源代码/.cpp.h)--&amp;gt;T(处理后文件/.i)         T--&amp;gt;F(汇编代码/.s)         F--&amp;gt;I(目标文件/.o)         I--&amp;gt;V(可执行程序)     end  subgraph 编译流程图   A((用户源代码))==&amp;gt;R(预处理器 cpp) Q((外部头文件))==&amp;gt;R  R">
<meta name="keywords" content="预处理,编译,汇编,链接">
<meta property="og:type" content="article">
<meta property="og:title" content="编译过程">
<meta property="og:url" content="http://yoursite.com/2019/06/30/编程语言/c++/编译过程/index.html">
<meta property="og:site_name" content="hexo">
<meta property="og:description" content="编译过程graph TB  subgraph 代码生成图         E(源代码/.cpp.h)--&amp;gt;T(处理后文件/.i)         T--&amp;gt;F(汇编代码/.s)         F--&amp;gt;I(目标文件/.o)         I--&amp;gt;V(可执行程序)     end  subgraph 编译流程图   A((用户源代码))==&amp;gt;R(预处理器 cpp) Q((外部头文件))==&amp;gt;R  R">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2019-07-09T03:12:46.311Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="编译过程">
<meta name="twitter:description" content="编译过程graph TB  subgraph 代码生成图         E(源代码/.cpp.h)--&amp;gt;T(处理后文件/.i)         T--&amp;gt;F(汇编代码/.s)         F--&amp;gt;I(目标文件/.o)         I--&amp;gt;V(可执行程序)     end  subgraph 编译流程图   A((用户源代码))==&amp;gt;R(预处理器 cpp) Q((外部头文件))==&amp;gt;R  R">
  
    <link rel="alternate" href="/atom.xml" title="hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.ico">
  
  
    
  
  <link rel="stylesheet" href="/css/style.css">
  

</head>
</html>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    
    <div id="header-inner" class="inner">
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">首页</a>
        
          <a class="main-nav-link" href="/archives">归档</a>
        
          <a class="main-nav-link" href="/categories/高等数学">高等数学</a>
        
          <a class="main-nav-link" href="/categories/线性代数">线性代数</a>
        
          <a class="main-nav-link" href="/categories/数据结构与算法">数据结构与算法</a>
        
          <a class="main-nav-link" href="/categories/English">英语</a>
        
          <a class="main-nav-link" href="/about">关于</a>
        
      </nav>
      
    </div>
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">hexo</a>
      </h1>
      
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-编程语言/c++/编译过程" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2019/06/30/编程语言/c++/编译过程/" class="article-date">
  <time datetime="2019-06-30T10:52:31.000Z" itemprop="datePublished">2019-06-30</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/编程语言/">编程语言</a>►<a class="article-category-link" href="/categories/编程语言/c/">c++</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      编译过程
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <!-- Table of Contents -->
        
        <h2 id="编译过程"><a href="#编译过程" class="headerlink" title="编译过程"></a>编译过程</h2><pre class="mermaid">graph TB

subgraph 代码生成图
        E(源代码/.cpp.h)-->T(处理后文件/.i)
        T-->F(汇编代码/.s)
        F-->I(目标文件/.o)
        I-->V(可执行程序)
    end

subgraph 编译流程图


A((用户源代码))==>R(预处理器 cpp)
Q((外部头文件))==>R

R==>U((替换后的代码))
U==>B(编译器 gcc/g++/clang) 

B==>D((汇编代码c))
B==>M((汇编代码b))
B==>N((汇编代码a))
D==>G(汇编器  as )
M==>G
N==>G
P(外部库文件)==>J(链接器 ld)
G==>L((目标文件a))
G==>K((目标文件b))
G==>H((目标文件c))

H==>J
K==>J
L==>J

J==>O((可执行程序))
J==>W((静态库/动态库))

style R fill:#FFC0CB;
style B fill:#FF69B4;
style G fill:#FF1493;
style J fill:#C71585;
end
A-->E
U-->T
D-->F
H-->I</pre>

<h2 id="编译器选项"><a href="#编译器选项" class="headerlink" title="编译器选项"></a>编译器选项</h2><pre class="mermaid">graph LR
A(g++)-->B(预处理)
B-->C( -E  预处理 宏/条件编译/头文件包含)
B-->D( -I 指定头文件搜索路径)
B-->J( -W 警告)
B-->R( -w 禁止警告)


A-->H(编译)
H-->V( -g 生成调试信息)
H-->X( -S 生成汇编代码 .s)

A-->W(汇编)
W-->K( -O 优化)
K-->L( -O0 不进行优化)
K-->M( -O 优化)
K-->N( -O2 -O3 进一步优化)

A-->S(链接)
S-->T( -l 链接指定的函数库)
S-->U( -L 链接指定的函数库搜索路径)


A-->O(生成目标文件)
O-->P( -share 生成共享目标文件.so,创建共享库)
O-->Q( -static 生成静态文件.a ,禁止共享链接)</pre>

<h2 id="命令执行步骤"><a href="#命令执行步骤" class="headerlink" title="命令执行步骤"></a>命令执行步骤</h2><pre class="mermaid">graph TB

subgraph 理论步骤
A(预处理 PreProcessing)==>B(编译 Compilation)
A-->I(处理过的代码)
B==>C(汇编 Assemble)
B-->J(汇编代码)
C==>D(链接 Linking )
C-->K(机器码)
D-->L(可执行代码)
D-->M(二进制库文件)
end

subgraph 实际步骤
E(cpp test.c -I./inc -o test.i)
E==>F(gcc -S -I./inc test.c -o test.s)
F==>G(as test.s -o test.o)
G==>H(ld -o app test.o inc/mymath.o -llib -I libraries )
end</pre>


      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2019/06/30/编程语言/c++/编译过程/" data-id="cjz7utmpe003exhpe8kvpm2dz" class="article-share-link">分享</a>
      
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/汇编/">汇编</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/编译/">编译</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/链接/">链接</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/预处理/">预处理</a></li></ul>

    </footer>
  </div>
  
    
    
<nav id="article-nav">
  
    <a href="/2019/07/01/编程语言/c++/变量类型取值范围/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          变量类型取值范围
        
      </div>
    </a>
  
  
    <a href="/2019/06/29/编程语言/c++/简介/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">C++简介</div>
    </a>
  
</nav>

  
</article>
 
     
  

</section>
           
    <aside id="sidebar">
  
    

  
    
    <div class="widget-wrap">
    
      <div class="widget" id="toc-widget-fixed">
      
        <strong class="toc-title">文章目录</strong>
        <div class="toc-widget-list">
              <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#编译过程"><span class="toc-number">1.</span> <span class="toc-text">编译过程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#编译器选项"><span class="toc-number">2.</span> <span class="toc-text">编译器选项</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#命令执行步骤"><span class="toc-number">3.</span> <span class="toc-text">命令执行步骤</span></a></li></ol>
          </div>
      </div>
    </div>

  
    

  
    
  
    
  
    

  
    
  
    <!--微信公众号二维码-->


  
</aside>

      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-left">
      &copy; 2014 - 2019 tiger&nbsp;|&nbsp;
      主题 <a href="https://github.com/giscafer/hexo-theme-cafe/" target="_blank">Cafe</a>
    </div>
     <div id="footer-right">
      联系方式&nbsp;|&nbsp;375478250@qq.com
    </div>
  </div>
</footer>
 <script src="/jquery/jquery.min.js"></script>

 <script src='https://unpkg.com/mermaid@8.1.0/dist/mermaid.min.js'></script>
 <script>
 if (window.mermaid) {
	 mermaid.initialize({startOnLoad:true});
 }
</script>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">首页</a>
  
    <a href="/archives" class="mobile-nav-link">归档</a>
  
    <a href="/categories/高等数学" class="mobile-nav-link">高等数学</a>
  
    <a href="/categories/线性代数" class="mobile-nav-link">线性代数</a>
  
    <a href="/categories/数据结构与算法" class="mobile-nav-link">数据结构与算法</a>
  
    <a href="/categories/English" class="mobile-nav-link">英语</a>
  
    <a href="/about" class="mobile-nav-link">关于</a>
  
</nav>
    <img class="back-to-top-btn" src="/images/fly-to-top.png"/>
<script>
// Elevator script included on the page, already.
window.onload = function() {
  var elevator = new Elevator({
    selector:'.back-to-top-btn',
    element: document.querySelector('.back-to-top-btn'),
    duration: 1000 // milliseconds
  });
}
</script>
    <!-- author:forvoid begin -->
<!-- author:forvoid end -->


  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      })
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      })
    </script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>
  


 <script src="/js/is.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>
<script src="/js/elevator.js"></script>
  </div>
</body>
</html>
